package cn.daniellee.plugin.ce.storage;

import cn.daniellee.plugin.ce.CraftEconomy;
import cn.daniellee.plugin.ce.model.PlayerData;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class YamlStorage extends Storage {

	private File dataFile = new File(CraftEconomy.getInstance().getDataFolder(), "player.yml");

	private FileConfiguration dataYaml = new YamlConfiguration();

	@Override
	public boolean initialize() {
		try {
			if (!dataFile.exists()) dataFile.createNewFile();
			dataYaml.load(dataFile);
		} catch (Exception e) {
			CraftEconomy.getInstance().getLogger().info(" ");
			CraftEconomy.getInstance().getLogger().info("[CraftEconomy]An error occurred in player / reward file load.".replace("&", "§"));
			CraftEconomy.getInstance().getLogger().info(" ");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public PlayerData refreshPlayerData(String targetPlayer) {
		PlayerData playerData = new PlayerData();
		if (CraftEconomy.getInstance().isOnlineMode()) {
			playerData.setUuid(targetPlayer);
			playerData.setName(dataYaml.getString(targetPlayer + ".name", null));
		} else {
			playerData.setName(targetPlayer);
			playerData.setUuid(dataYaml.getString(targetPlayer + ".uuid", null));
		}
		double balance = dataYaml.getDouble(targetPlayer + ".balance", -1);
		if (balance == -1) {
			double defaultBalance = CraftEconomy.getInstance().getConfig().getDouble("setting.initial-balance", 500.00D);
			balance = defaultBalance;
			dataYaml.set(targetPlayer + ".balance", defaultBalance);
			saveDataYaml();
		}
		playerData.setBalance(balance);
		allPlayerDataCache.put(targetPlayer, playerData);
		return playerData;
	}

	@Override
	public void updatePlayerData(String targetPlayer, String column, Object value) {
		PlayerData playerData = getPlayerData(targetPlayer);
		switch (column) {
			case "uuid":
				dataYaml.set(targetPlayer + ".uuid", value);
				playerData.setUuid((String) value);
				break;
			case "name":
				dataYaml.set(targetPlayer + ".name", value);
				playerData.setName((String) value);
				break;
			case "balance":
				dataYaml.set(targetPlayer + ".balance", value);
				playerData.setBalance((Double) value);
				break;
		}
		saveDataYaml();
	}

	@Override
	public List<PlayerData> getAllPlayerData() {
		List<PlayerData> allPlayerData = new ArrayList<>();
		Set<String> uuidOrNames = dataYaml.getKeys(false);
		for (String uuidOrName : uuidOrNames) {
			PlayerData playerData = allPlayerDataCache.get(uuidOrName);
			if (playerData == null) {
				playerData = refreshPlayerData(uuidOrName);
			}
			allPlayerData.add(playerData);
		}
		return allPlayerData;
	}

	private void saveDataYaml() {
		try {
			dataYaml.save(dataFile);
		} catch (IOException e) {
			CraftEconomy.getInstance().getLogger().info(" ");
			CraftEconomy.getInstance().getLogger().info("[CraftEconomy]An error occurred in player file save.".replace("&", "§"));
			CraftEconomy.getInstance().getLogger().info(" ");
			e.printStackTrace();
		}
	}
}
